package com.zyd.blog.personal.service.impl;
import	java.util.HashMap;
import java.util.*;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.service.MerchantApplyDealService;
import com.zyd.blog.personal.service.MerchantInfoService;
import com.zyd.blog.personal.utils.DateUtils;
import com.zyd.blog.personal.utils.JavaBeanAndMapUtils;
import com.zyd.blog.personal.utils.StringUtils;
import com.zyd.blog.personal.vo.MerchantApplyInfoConditionVo;
import com.zyd.blog.personal.vo.ReviewResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 商家合作申请审核
 * @author mfy
 */
@Service
public class MerchantApplyDealServiceImpl implements MerchantApplyDealService {

    @Resource
    private MerchantInfoService merchantInfoService;

    /**
     * 格式化查询条件
     * @param condition  查询条件对象
     * @return  格式化后的条件
     */
    private List<Map<String,Object>> getMerchantApplyInfoList(Map<String,Object> condition){
        condition.put("applyTimeStart", DateUtils.parseStringToLong((String) condition.get("applyTimeStart")));
        condition.put("applyTimeEnd", DateUtils.parseStringToLong((String) condition.get("applyTimeEnd")));
        condition.put("isDelete", IsDelete.IS_DELETE_NO);
        return merchantInfoService.queryMerchantInfoByCondition(condition);
    }

    /**
     * 对结果进行格式化
     * @param merchantApplyInfos    结果
     * @return  格式化后的数据
     */
    private List<Map<String,Object>> formatMerchantInfoList(List<Map<String,Object>> merchantApplyInfos){
        if(merchantApplyInfos == null || merchantApplyInfos.size() == 0){
            return new ArrayList<>();
        }
        List<Map<String,Object>> result = new ArrayList<>();
        for (Map<String, Object> item : merchantApplyInfos){
            item.put("licensePicture", StringUtils.getStringOfSplit((String) item.get("licensePicture")));
            item.put("logo", StringUtils.getStringOfSplit((String) item.get("logo")));
            String step = (String) item.get("step");
            switch (step){
                case "2":
                    item.put("stepMsg","待审核");
                    break;
                case "3":
                    item.put("stepMsg","审核通过");
                    break;
                case "4":
                    item.put("stepMsg","审核不通过");
                    break;
                    default :break;
            }
            String status = (String) item.get("status");
            switch (status){
                case "1":
                    item.put("statusMsg","启用");
                    break;
                case "0":
                    item.put("statusMsg","禁用");
                    break;
                default:
                    break;
            }
            item.put("applyTime",DateUtils.parseLongToString((Long) item.get("applyTime"),"yyyy-MM-dd HH:mm:ss"));
            item.put("reviewTime",DateUtils.parseLongToString((Long) item.get("reviewTime"),"yyyy-MM-dd HH:mm:ss"));
            item.remove("isDelete");
            item.remove("updateTime");
            item.remove("addTime");
            result.add(item);
        }
        return result;
    }

    @Override
    public List<Map<String,Object>> getMerchantApplyInfoList(MerchantApplyInfoConditionVo merchantApplyInfoConditionVo){
        List<Map<String,Object>> merchantApplyInfos = getMerchantInfoList(merchantApplyInfoConditionVo);
        List<Map<String,Object>> result = new ArrayList<>();
        for (Map<String,Object> item : merchantApplyInfos){
            if("3".equals(item.get("step"))){
                continue;
            }
            result.add(item);
        }
        return result;
    }

    @Override
    public List<Map<String,Object>> getMerchantList(MerchantApplyInfoConditionVo merchantApplyInfoConditionVo){
        merchantApplyInfoConditionVo.setStep("3");
        return getMerchantInfoList(merchantApplyInfoConditionVo);
    }


    @Override
    public List<Map<String,Object>> getMerchantInfoList(MerchantApplyInfoConditionVo merchantApplyInfoConditionVo){
        Map<String,Object> condition = JavaBeanAndMapUtils.javaBeanToMap(merchantApplyInfoConditionVo);
        List<Map<String,Object>> merchantApplyInfos = getMerchantApplyInfoList(condition);
        List<Map<String,Object>> result = formatMerchantInfoList(merchantApplyInfos);
        if(result.size() == 0){
            return new ArrayList<>();
        }
        return result;
    }

    private Map<String,Object> formatUnitMerchantInfo(Map<String,Object> merchantApplyInfo){
        if(merchantApplyInfo == null || merchantApplyInfo.size() == 0){
            return new HashMap<> (0);
        }
        String step = (String) merchantApplyInfo.get("step");
        switch (step){
            case "2":
                merchantApplyInfo.put("result","待审核");
                break;
            case "4":
                merchantApplyInfo.put("result","审核不通过");
                break;
            default :break;
        }
        merchantApplyInfo.put("licensePicture",StringUtils.getStringOfSplit((String) merchantApplyInfo.get("licensePicture")));
        merchantApplyInfo.put("logo",StringUtils.getStringOfSplit((String) merchantApplyInfo.get("logo")));
        merchantApplyInfo.put("applyTime",DateUtils.parseLongToString((Long) merchantApplyInfo.get("applyTime"),"yyyy-MM-dd HH:mm:ss"));
        merchantApplyInfo.put("reviewTime",DateUtils.parseLongToString((Long) merchantApplyInfo.get("reviewTime"),"yyyy-MM-dd HH:mm:ss"));
        merchantApplyInfo.remove("isDelete");
        merchantApplyInfo.remove("updateTime");
        merchantApplyInfo.remove("addTime");
        return merchantApplyInfo;
    }

    @Override
    public Map<String,Object> getDetailMerchantApplyInfo(Long id){
        if(id == null){
            return new HashMap<> (0);
        }
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("id",id);
        List<Map<String,Object>> merchantApplyInfos = getMerchantApplyInfoList(condition);
        if(merchantApplyInfos.size() == 0){
            return new HashMap<> (0);
        }
        return formatUnitMerchantInfo(merchantApplyInfos.get(0));
    }

    /**
     * 审核商家合作信息
     * @param reviewResultVo    商家合作信息
     * @return  操作结果
     */
    private boolean reviewMerchantInfo(ReviewResultVo reviewResultVo){
        Map<String,Object> condition = new HashMap<> (16);
        if(reviewResultVo.getId() == null){
            return false;
        }
        condition.put("id",reviewResultVo.getId());
        String step = reviewResultVo.getStep();
        condition.put("step",step);
        switch (step){
            case "3":
                condition.put("reason","审核通过");
                break;
            case "4":
                condition.put("reason",reviewResultVo.getReason());
                break;
                default :break;
        }
        condition.put("reviewName",reviewResultVo.getReviewer());
        condition.put("reviewTime",(new Date()).getTime());
        return merchantInfoService.updateMerchantInfo(condition);
    }

    @Override
    public boolean review(ReviewResultVo reviewResultVo){
        if(reviewResultVo.getId() == null){
            return false;
        }
        return reviewMerchantInfo(reviewResultVo);
    }

    @Override
    public boolean delete(Long id){
        if(id == null){
            return false;
        }
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("id",id);
        condition.put("isDelete",IsDelete.IS_DELETE_YES);
        return merchantInfoService.updateMerchantInfo(condition);
    }
    @Override
    public boolean changeStatus(Long id,String status){
        if(id == null){
            return false;
        }
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("id",id);
        condition.put("status",status);
        return merchantInfoService.updateMerchantInfo(condition);
    }
}
